use std::env;
use std::path::Path;
use winreg::enums::{HKEY_LOCAL_MACHINE, KEY_READ, KEY_SET_VALUE};
use winreg::RegKey;

fn main() {
    // 替换为你要添加的路径
    let new_path = r"C:\Your\New\Path";

    // 获取当前的PATH变量
    let mut current_path = env::var("PATH").unwrap_or_default();

    // 检查路径是否已存在
    if !current_path.contains(new_path) {
        // 添加新的路径
        current_path.push_str(";");
        current_path.push_str(new_path);

        // 设置新的PATH变量到注册表
        if let Ok(reg_key) = RegKey::predef(HKEY_LOCAL_MACHINE).create_subkey_with_flags(
            r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment",
            KEY_SET_VALUE,
        ) {
            reg_key.0//.set_value(name, value)
                .set_value("Path", &current_path)
                .expect("Failed to set PATH variable in registry.");
            println!("Path added successfully.");
        } else {
            eprintln!("Failed to open registry key.");
        }
    } else {
        println!("Path already exists in the PATH variable.");
    }
}